//
//  LGTRWZImageTextStepCell.swift
//  KJLGT
//
//  Created by jenkin on 2019/8/24.
//  Copyright © 2019年 jenkin. All rights reserved.
//

import UIKit

class LGTRWZPostDetailImageTextStepCell: UITableViewCell {
    
    var bottomClickBlock:ClickIndexBlock?
    var titleLabel:UILabel?
    var stepLabel:UILabel?
    var typeLabel:UILabel?
    var checkImageView:UIImageView?
    var addImageView:UIImageView? //添加上传验证图
    var descLabel:UILabel?
    private var _model:Dictionary<String, Any>?
    var path:IndexPath?
    var copyBtn:UIButton?
    var stepCellClickBlock:ClickStepPathBlock?
    var descArray:Array<String> = ["验证图", "说明图", "上传二维码"]
    var model:Dictionary<String, Any>{
        get{
            return _model ?? Dictionary.init()
        }set{
            _model = newValue
            self.setData()
        }
    }
    
    func setData(){
        self.titleLabel?.text = self.model["stepText"] as? String ?? ""
        let selectInfo:Dictionary<String, Any> = self.model["selectInfo"] as? Dictionary<String, Any> ?? [:]
        let url:String = selectInfo["url"] as? String ?? ""
        BaseUtil.baseUtil.setImage(imageView: self.checkImageView!, imageUrl: url, type: .BigImageType)
        
        //提交订单的参数显示
        let submitInfo:Dictionary<String, Any>? = self.model["submitInfo"] as? Dictionary<String, Any>
        if submitInfo != nil{
            let url2:String = submitInfo?["url"] as? String ?? ""
            //self.addImageView?.sd_setImage(with: URL.init(string: url2), completed: nil)
            BaseUtil.baseUtil.setImage(imageView: self.addImageView!, imageUrl: url2, type: .BigImageType)
        }
        
        //let type:Int = self.model["type"] as? Int ?? 0
        self.descLabel?.isHidden = false
        self.addImageView?.isHidden = true
        let canEdit:Bool = self.model["canEdit"] as? Bool ?? false
        
//        let type:Int = self.model["type"] as? Int ?? 0
//        if type == 2{
//            self.descLabel?.isHidden = true
//        }
//        if type <= self.descArray.count - 1{
//            self.descLabel?.text = self.descArray[type]
//        }
//
//        if type == 0 && canEdit == true{
//             self.addImageView?.isHidden = false
//        }
        
        
        //        //0上传验证图  1上传说明图 2上传二维码
        //     index       5        4        2
        let index:Int = self.model["type"] as? Int ?? 0
        self.descLabel?.isHidden = false
        if index == 2{
            self.descLabel?.isHidden = true
            self.descLabel?.text = "二维码"
        } else if index == 4{
            self.descLabel?.text = "说明图"
        }else{
            self.descLabel?.text = "验证图"
            if canEdit == true{
                self.addImageView?.isHidden = false
            }
        }
        
        self.stepLabel?.text = "\((self.path?.row ?? 1))"
        //var type:Int = 0  //0上传验证图  1上传说明图 2上传二维码
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
        // Configure the view for the selected state
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = UITableViewCell.SelectionStyle.none
        
        //self.isUserInteractionEnabled = false
        self.titleLabel = LGTViewCreateUtil.share.createLabel(textAlignment: NSTextAlignment.left, textColor: TextColor, fontSize: SizeFitFloat(15), text: "扫描下方二维码")
        self.titleLabel?.numberOfLines = 0
        self.addSubview(self.titleLabel!)
        self.titleLabel!.snp.makeConstraints { (make) in
            make.left.equalTo(SizeFitFloat(41))
            make.right.equalTo(-SizeFitFloat(48))
            make.top.equalTo(SizeFit(22))
        }
        
        self.stepLabel =  LGTViewCreateUtil.share.createLabel(textAlignment: NSTextAlignment.center, textColor: UIColor.white, fontSize: SizeFitFloat(16), text: "1", corner: SizeFitFloat(10))
        self.addSubview(self.stepLabel!)
        self.stepLabel?.backgroundColor = YellowColor
        self.stepLabel!.snp.makeConstraints { (make) in
            make.left.equalTo(SizeFit(13))
            make.height.width.height.equalTo(SizeFit(20))
            make.centerY.equalTo(self.titleLabel!)
        }
        
        self.checkImageView = LGTViewCreateUtil.share.createImageViewCorner(corner: SizeFitFloat(5))
        self.checkImageView?.backgroundColor = ViewBackColor
        self.checkImageView?.tag = StepCellClickType.ImageBrower.rawValue
        self.addSubview(self.checkImageView!)
        let backTop = SizeFit(12)
        self.checkImageView!.snp.makeConstraints { (make) in
            make.left.equalTo(SizeFitFloat(42))
            make.height.equalTo(SizeFitFloat(165))
            make.width.equalTo(SizeFitFloat(128))
            //make.top.equalTo(self.titleLabel!.snp_bottomMargin).offset(SizeFit(12))
            //make.top.equalTo(self.self.titleLabel!.safeAreaLayoutGuide.snp.bottom).offset(backTop)
            if #available(iOS 11.0, *){
                make.top.equalTo(self.self.titleLabel!.safeAreaLayoutGuide.snp.bottom).offset(backTop)
            }else{
                make.top.equalTo(self.self.titleLabel!.snp_bottomMargin).offset(backTop)
            }
           // make.top.equalTo(SizeFit(55))
        }
        self.checkImageView?.isUserInteractionEnabled = true
        
        self.addImageView = LGTViewCreateUtil.share.createImageViewCorner(corner: SizeFitFloat(5), imageName: "addto")
        self.addImageView?.tag = StepCellClickType.addCheckImage.rawValue
        self.addImageView?.backgroundColor = ViewBackColor
        self.addImageView?.isHidden = true
        self.addSubview(self.addImageView!)
        self.addImageView!.snp.makeConstraints { (make) in
            make.left.equalTo(self.checkImageView!.snp_rightMargin).offset(SizeFit(25))
            make.height.equalTo(SizeFitFloat(165))
            make.width.equalTo(SizeFitFloat(128))
            //make.top.equalTo(self.titleLabel!.snp_bottomMargin).offset(SizeFit(12))
            //make.top.equalTo(SizeFit(55))
            //make.top.equalTo(self.self.titleLabel!.safeAreaLayoutGuide.snp.bottom).offset(backTop)
            if #available(iOS 11.0, *){
                make.top.equalTo(self.self.titleLabel!.safeAreaLayoutGuide.snp.bottom).offset(backTop)
            }else{
                make.top.equalTo(self.self.titleLabel!.snp_bottomMargin).offset(backTop)
            }
        }
        self.addImageView?.isUserInteractionEnabled = true
        let tap:UITapGestureRecognizer = UITapGestureRecognizer.init(target: self, action: #selector(addImageAction))
        self.addImageView?.addGestureRecognizer(tap)
        
        let tap2:UITapGestureRecognizer = UITapGestureRecognizer.init(target: self, action: #selector(addImageAction))
        self.checkImageView?.addGestureRecognizer(tap2)
        
        let checkDescLabel = LGTViewCreateUtil.share.createLabel(textAlignment: NSTextAlignment.center, textColor: UIColor.white, fontSize: SizeFitFloat(12), text: "上传验证图")
        self.addImageView!.addSubview(checkDescLabel)
        checkDescLabel.backgroundColor = BlackAlpha46
        checkDescLabel.snp.makeConstraints { (make) in
            make.left.right.bottom.equalTo(self.addImageView!)
            make.height.equalTo(SizeFitFloat(28))
        }
        
        self.descLabel = LGTViewCreateUtil.share.createLabel(textAlignment: NSTextAlignment.center, textColor: UIColor.white, fontSize: SizeFitFloat(12), text: "说明图")
        self.checkImageView!.addSubview(self.descLabel!)
        self.descLabel?.backgroundColor = BlackAlpha46
        self.descLabel!.snp.makeConstraints { (make) in
            make.left.right.bottom.equalTo(self.checkImageView!)
            make.height.equalTo(SizeFitFloat(28))
        }
        
        let imageLine:UIImageView = LGTViewCreateUtil.share.createLineView()
        self.addSubview(imageLine)
        imageLine.backgroundColor = ColorUtil(color: 0xEFEFEF)
        let backTop2 = SizeFit(14)
        imageLine.snp.makeConstraints { (make) in
            make.left.equalTo(SizeFit(13))
            make.right.equalTo(-SizeFit(11))
            make.height.equalTo(1)
            //make.top.equalTo(self.checkImageView!.snp_bottomMargin).offset(SizeFit(14))
            //make.top.equalTo(self.self.checkImageView!.safeAreaLayoutGuide.snp.bottom).offset(backTop2)
            if #available(iOS 11.0, *){
                make.top.equalTo(self.self.checkImageView!.safeAreaLayoutGuide.snp.bottom).offset(backTop2)
            }else{
                make.top.equalTo(self.self.checkImageView!.snp_bottomMargin).offset(backTop2)
            }
            make.bottom.equalTo(0)
        }
    }
    
    func gotoActionBlock(index:Int){
        if self.stepCellClickBlock != nil{
            self.stepCellClickBlock!(self.model, self.path!, index, nil)
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc func addImageAction(tap:UITapGestureRecognizer){
        //print("上传图片")
        if self.stepCellClickBlock != nil{
            self.stepCellClickBlock!(self.model, self.path!, tap.view?.tag ?? -1, tap.view)
        }
    }
}

